<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 深圳市中南网络科技有限公司 [ http://www.znwlsz.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://www.znwlsz.com
// +----------------------------------------------------------------------

namespace app\admin\controller;

use app\common\builder\ZBuilder;
use app\common\model\Shiti as ST;
use think\Db;
/**
 * 辅食控制器
 * @package app\admin\controller
 */
class Shiti extends Admin
{
 
    public function index($group = 'tab1')
    {

            // jieduan type

        $add_panduan = [
            'title' => '添加判断题',
            'href'  => url('shiti/add_panduan',['id'=>'__id__'])
        ];

        $add_danxuan = [
            'title' => '添加单选题',
            'href'  => url('shiti/add_danxuan',['id'=>'__id__'])
        ];

        $add_duoxuan = [
            'title' => '添加多选题',
            'href'  => url('shiti/add_duoxuan',['id'=>'__id__'])
        ];

        $xiugai = [
            'title' => '编辑',
            'href'  => url('shiti/xiugai',['id'=>'__id__'])
        ];

        if($group=='tab1'){
            $where['type'] = '1';
        }elseif ($group=='tab2') {
            $where['type'] = '2';
        }elseif($group=='tab3'){
            $where['type'] = '3';
        }

        $daiwanshan = ST::where('type','1')->count();
        $yiwanshan = ST::where('type','2')->count();
        $daizhizuo = ST::where('type','3')->count();
        

        $list_tab = [
            'tab1' => ['title' => '单选题('.$daiwanshan.')', 'url' => url('index', ['group' => 'tab1'])],
            'tab2' => ['title' => '多选题('.$yiwanshan.')', 'url' => url('index', ['group' => 'tab2'])],
            'tab3' => ['title' => '判断题('.$daizhizuo.')', 'url' => url('index', ['group' => 'tab3'])],
            
        ];

        $data_list = ST::where($where)
            ->order($this->getOrder('id DESC'))
            ->paginate();

        return ZBuilder::make('table')
            ->addTopButton('custom', $add_danxuan) // 添加顶部按钮
            ->addTopButton('custom', $add_duoxuan) // 添加顶部按钮
            ->addTopButton('custom', $add_panduan) // 添加顶部按钮
            
            ->setTabNav($list_tab,  $group)
            ->addColumns([
                ['id', 'ID'],
                ['ctitle','试题标题'],
                ['create_time', '创建时间', 'datetime'],
                ['right_button', '操作', 'btn'],
            ])
            ->setTableName('shiti')
            // ->autoAdd($fields, 'shiti', true, true,'','',true) // 添加新增按钮
            // ->autoEdit($fields, 'shiti', true, true,'','',[],true) 
            ->addRightButton('custom', $xiugai,true, ['title' => true, 'icon' => false])
            
            ->addRightButton('delete')

            ->setRowList($data_list)
            ->fetch();
    }

    public function xiugai($id = 0){
        if ($id === 0) $this->error('参数错误');

        // 保存数据
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();

            // 验证
            $result = $this->validate($data, 'shiti');
            if(true !== $result) $this->error($result);

            if($data['type']=='2'){
                $data['answer'] = json_encode($data['canswer']);
            }
            unset($data['canswer']);
            if (Db::name('shiti')->update($data)) {
                $this->success('添加成功', null, '_parent_reload');
            } else {
                $this->error('上传失败');
            }
        }
        $info = Db::name('shiti')->where('id',$id)->find();

        if($info['type']=='1'){
            $fields = [
                ['hidden', 'id'],
                ['text','ctitle','题目'],
                ['text','A','A选项'],
                ['text','B','B选项'],
                ['text','C','C选项'],
                ['text','D','D选项'],
              
                ['radio', 'answer', '正确答案','',['A'=>'A', 'B'=>'B','C'=>'C','D'=>'D'],$info['answer']],

                ['hidden', 'type', 1],
                ['hidden', 'create_time', time()],
            ];
        }else if ($info['type']=='2') {
            $fields = [
                ['hidden', 'id'],
                ['text','ctitle','题目'],
                ['text','A','A选项'],
                ['text','B','B选项'],
                ['text','C','C选项'],
                ['text','D','D选项'],
                ['hidden', 'type', 2],
                ['hidden', 'create_time', time()],
            ];
        }else{
            $fields = [
                ['hidden', 'id'],
                ['text','ctitle','题目'],
              
                ['radio', 'answer', '正确答案','',['0'=>'对', '1'=>'错'],'0'],

                ['hidden', 'type', 3],
                ['hidden', 'create_time', time()],
            ];
        }
        $answer = ['A' => 'A', 'B' => 'B', 'C' => 'C', 'D' => 'D'];
        // halt(json_decode($info['answer'],true));
        // halt(['A','B','C']);

        if($info['type']=='2'){
            $answerb = json_decode($info['answer'],true);
            // halt($answer);
            // 使用ZBuilder快速创建表单
            return ZBuilder::make('form')
            ->setPageTitle('修改查看')
            ->addFormItems($fields)
            ->addCheckbox('canswer', '正确答案', '请选择', $answer, $answerb)
            ->setFormData($info)
            ->fetch();
        }else{
            // 使用ZBuilder快速创建表单
            return ZBuilder::make('form')
            ->setPageTitle('修改查看')
            ->addFormItems($fields)
            ->setFormData($info)
            ->fetch();
        }
        
    }

    public function add_panduan($id = 0)
    {
        if ($id === 0) $this->error('参数错误');

        // 保存数据
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();

            // 验证
            $result = $this->validate($data, 'shiti');
            if(true !== $result) $this->error($result);

            if (Db::name('shiti')->insert($data)) {
                $this->success('添加成功', null, '_parent_reload');
            } else {
                $this->error('上传失败');
            }
        }
         // 获取数据

        $fields = [
            ['hidden', 'id'],
            ['text','ctitle','题目'],
          
            ['radio', 'answer', '正确答案','',['0'=>'对', '1'=>'错'],'0'],

            ['hidden', 'type', 3],
            ['hidden', 'create_time', time()],
        ];

        // 使用ZBuilder快速创建表单
        return ZBuilder::make('form')
            ->setPageTitle('判断题添加')
            ->addFormItems($fields)
            ->fetch();
    }


    public function add_duoxuan($id = 0)
    {
        if ($id === 0) $this->error('参数错误');

        // 保存数据
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();

            // 验证
            $result = $this->validate($data, 'shiti');
            if(true !== $result) $this->error($result);
            $data['answer'] = json_encode($data['answer']);
            if (Db::name('shiti')->insert($data)) {
                $this->success('添加成功', null, '_parent_reload');
            } else {
                $this->error('上传失败');
            }
        }
         // 获取数据

        $fields = [
            ['hidden', 'id'],
            ['text','ctitle','题目'],
            ['text','A','A选项'],
            ['text','B','B选项'],
            ['text','C','C选项'],
            ['text','D','D选项'],
            ['checkbox', 'answer', '正确答案','',['A'=>'A', 'B'=>'B','C'=>'C','D'=>'D'],'A'],

            ['hidden', 'type', 2],
            ['hidden', 'create_time', time()],
        ];

        // 使用ZBuilder快速创建表单
        return ZBuilder::make('form')
            ->setPageTitle('多选题添加')
            ->addFormItems($fields)
            ->fetch();
    }

    public function add_danxuan($id = 0)
    {
        if ($id === 0) $this->error('参数错误');

        // 保存数据
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();

            // 验证
            $result = $this->validate($data, 'shiti');
            if(true !== $result) $this->error($result);


            if (Db::name('shiti')->insert($data)) {
                $this->success('添加成功', null, '_parent_reload');
            } else {
                $this->error('上传失败');
            }
        }
         // 获取数据

        $fields = [
            ['hidden', 'id'],
            ['text','ctitle','题目'],
            ['text','A','A选项'],
            ['text','B','B选项'],
            ['text','C','C选项'],
            ['text','D','D选项'],
            ['radio', 'answer', '正确答案','',['A'=>'A', 'B'=>'B','C'=>'C','D'=>'D'],'A'],
            ['hidden', 'type', 1],
            ['hidden', 'create_time', time()],
        ];

        // 使用ZBuilder快速创建表单
        return ZBuilder::make('form')
            ->setPageTitle('单选题添加')
            ->addFormItems($fields)
            ->fetch();
    }

}